package com.sunsys.mapreduce.distributedcache.example1;

import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class LoadJarIntoClassPathUsingDistributedCacheDriver {

	public LoadJarIntoClassPathUsingDistributedCacheDriver() {
	}
	
	
	public int run(String[] arg0) throws Exception {
		String inputFile = "/usr/drools/input/BookRatings.txt";
		String outputPath = "/usr/cacheexample/output/1";
		String jarFilePath = "/usr/cacheexample/jar/com.amex.grms.formater-0.0.1-SNAPSHOT.jar";
		
		
		Job job = new Job();
		
		job.setJobName("LoadJarIntoClassPathUsingDistributedCache");
		job.setJarByClass(LoadJarIntoClassPathUsingDistributedCacheDriver.class);
				
		job.setMapperClass(DistrbuteCacheMapper.class);
		job.setNumReduceTasks(0);

		FileInputFormat.addInputPath(job, new Path(inputFile));
		FileOutputFormat.setOutputPath(job, new Path(outputPath));

		job.setMapOutputValueClass(NullWritable.class);
		job.setOutputValueClass(Text.class);
		
		/* add the jar file to distributed chache (jar should be present in HDFS, and path is of hdfs path */
		DistributedCache.addFileToClassPath(new Path(jarFilePath), job.getConfiguration());
		
		boolean result = job.waitForCompletion(true);

		System.out.println("result :"+result);
		System.out.println("Thank you");
		return 0;
	}
	
	
	public static void main(String []args) throws Exception {
		
		LoadJarIntoClassPathUsingDistributedCacheDriver driver = new LoadJarIntoClassPathUsingDistributedCacheDriver();
		driver.run(args);
	}
}
